<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="路由点" width="200px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.router }}</span>
        </template>
      </el-table-column>
      <el-table-column label="路由模式" width="200px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.routeMode | modeFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手动路由决策" class-name="status-col" width="300px">
        <template slot-scope="scope">
          <span>{{ modeDesFilter(scope.row.modeDes) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="autoOperate(row.id)">
            自动
          </el-button>
          <el-button type="warning" size="mini" @click="manualOperate(row)">
            手动
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="'手动模式路由决策选择'" :visible.sync="dialogFormVisible" :width="'400px'">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="80px" style="width: 300px; margin-left:50px;">
        <el-form-item label="路由决策" prop="modeDes">
          <el-select v-model="temp.modeDes" class="filter-item" placeholder="请选择">
            <el-option v-for="item in manualMOdeDesList" :key="item.id" :label="item.value" :value="item.id" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          退出
        </el-button>
        <el-button type="primary" @click="manualSubmit">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { fetchList, fetchModeList, auto, manual } from '@/api/bdc/route-mode-ctrl'

export default {
  name: 'RouteModeCtrl',
  filters: {
    modeFilter(mode) {
      const modeMap = {
        0: '自动模式',
        1: '手动模式',
      }
      return modeMap[mode]
    }
  },
  data() {
    return {
      // 表格显示
      tableKey: 0,
      list: null,
      listLoading: false,
      modeDesList: [],
      // 手动决策模态窗
      manualMOdeDesList: [],
      temp: { 
        id: undefined, 
        router: '', 
        routeMode: 0, 
        modeDes: '' 
      },
      dialogFormVisible: false,
      rules: {
        modeDes: [{ required: true, message: '必填', trigger: 'change' }]
      },
    }
  },
  created() {
    // 获取路由决策列表
    fetchModeList().then(res => {
      if (res.code == 20000) {
        this.modeDesList = res.data
        this.getList()
      } else {
        this.$message.error('服务器内部错误！');
      }
      this.listLoading = false
    })
    
  },
  methods: {
    // 获取路由列表
    getList() {
      this.listLoading = true
      fetchList().then(res => {
        // console.log(res);
        if (res.code == 20000) {
          this.list = res.data
        } else {
          this.$message.error('服务器内部错误！');
        }
        this.listLoading = false
      })
    },
    // 路由决策匹配
    modeDesFilter(mode) {
      if (mode === '') {
        return ''
      } else {
        return this.modeDesList.find(a => a.id === mode).value
      }
    },
    // 自动路由决策
    autoOperate(id) {
      auto(id).then((res) => {
        console.log('auto', res);
        if (res.code === 20000) {
          this.$message.success('自动模式设置成功！')
          this.getList()
        } else {
          this.$message.error('服务器内部错误')
        }
      })
    },
    manualOperate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogFormVisible = true
      // 随机select值
      const i = Math.floor(Math.random() * 10)
      this.manualMOdeDesList = this.modeDesList.filter((a, index) => a.id === this.temp.modeDes || index % i === 0);
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
      
    },
    manualSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          manual(tempData).then((res) => {
            if (res.code === 20000) {
              this.dialogFormVisible = false
              this.$message.success('手动模式设置成功！')
              this.getList()
            } else {
              this.dialogFormVisible = false
              this.$message.error(res.message)
            }
          })
        }
      })
    }
  }
}
</script>